using System;
using System.Transactions;

namespace SimpleServiceBus.Utilities
{
    /// <summary>
    /// Provides functionality for executing a callback in a transaction.
    /// </summary>
    public class TransactionWrapper
    {
        /// <summary>
        /// Executes the provided delegate method in a transaction.
        /// </summary>
        /// <param name="callback">The delegate method to call.</param>
        public void RunInTransaction(Action callback)
        {
            RunInTransaction(callback, TransactionScopeOption.Required);
        }

        /// <summary>
        /// Executes the provided delegate method in a transaction.
        /// </summary>
        /// <param name="callback">The delegate method to call.</param>
        public void RunInTransaction(Action callback,TransactionScopeOption transactionScopeOption)
        {
            using (var scope = new TransactionScope(transactionScopeOption))
            {
                callback();

                scope.Complete();
            }
        }
    }
}